package com.xiaoyg.algorithm.lru;

public class DoubleList {

    private Node head, tail;

    private int size;

    public DoubleList(){
        head = new Node(0,0);
        tail = new Node(0,0);
        head.next = tail;
        tail.prev = head;
        size = 0;
    }


    /**
     * 末尾添加结点
     * @param x
     */
    public  void addLast(Node x){
        //x结点 的prev和next
        x.prev = tail.prev;
        x.next = tail;

        //上一个结点的next，tail结点的prev
        tail.prev.next = x;
        tail.prev = x;

        size ++;
    }

    public  void remove(Node x){
        x.prev.next = x.next;
        x.next.prev = x.prev;
        size--;
    }

    //移除头结点
    public Node removeFirst(){
        if (head.next == null){
            return null;
        }
        Node first = head.next;
        remove(first);
        return first;
    }


    public int size(){
        return size;
    }

}
